package com.ita.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ita.service.MUserService;
import com.ita.util.Constant;
import com.ita.util.JsonResult;

@RestController
@RequestMapping("api/muser")
@CrossOrigin
public class APIAdminController {

	@Autowired(required = true)
	private MUserService mService;

	@RequestMapping(value = "/{mid}/block/{block}", method = RequestMethod.PATCH)
	public JsonResult updateBlockById(@PathVariable("mid") String mid, @PathVariable("block") int block) {
		JsonResult result = null;
		if (mid == null || "".equals(mid)) {
			result = new JsonResult(Constant.FAIL, "mid is null.", -1);
			return result;
		}
		if (!(block >= 0 && block <= 1)) {
			result = new JsonResult(Constant.FAIL, "block state is invalid", -1);
			return result;
		}
		int m = mService.updateBlock(mid, block);
		if (m == 1) {
			result = new JsonResult(Constant.SUCCESS, "success", block);
		} else {
			result = new JsonResult(Constant.FAIL, "MID cannot found this user", -1);
		}
		return result;
	}

}
